package com.whfc.fse.config;


import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class RabbitConfig {


    @Bean(name = "baseConnectionFactory")
    @Primary
    public ConnectionFactory baseConnectionFactory(
            @Value("${spring.rabbitmq.host}") String host,
            @Value("${spring.rabbitmq.port}") Integer port,
            @Value("${spring.rabbitmq.username}") String username,
            @Value("${spring.rabbitmq.password}") String password
    ) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();

        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }


    @Bean(name = "baseRabbitTemplate")
    @Primary
    public RabbitTemplate baseRabbitTemplate(
            @Qualifier("baseConnectionFactory") ConnectionFactory connectionFactory
    ) {
        RabbitTemplate baseRabbitTemplate = new RabbitTemplate(connectionFactory);
        return baseRabbitTemplate;
    }

    @Bean(name = "baseFactory")
    public SimpleRabbitListenerContainerFactory baseFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("baseConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }




    /**
     * 升思mq
     * @return
     */
    @Bean(name = "shengSiConnectionFactory")
    public ConnectionFactory shengSiConnectionFactory(
    ) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost("122.189.98.178");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("external");
        connectionFactory.setPassword("72fs32d");
        return connectionFactory;
    }


    @Bean(name = "shengSiRabbitTemplate")
    public RabbitTemplate shengSiRabbitTemplate(
            @Qualifier("shengSiConnectionFactory") ConnectionFactory connectionFactory
    ) {
        RabbitTemplate shengSiRabbitTemplate = new RabbitTemplate(connectionFactory);
        return shengSiRabbitTemplate;
    }

    @Bean(name = "shengSiFactory")
    public SimpleRabbitListenerContainerFactory shengSiFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("shengSiConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }


}
